package io.wongxd.solution.util.network

import kotlin.math.exp
import kotlin.math.ln

class ExponentialMovingAverage(private val decay: Float) {
    private var value = -1.0

    fun addMeasurement(measurement: Double) {
        value = if (value < 0.0) {
            measurement
        } else {
            exp((1 - decay) * ln(value) + decay * ln(measurement))
        }
    }

    fun getAverage(): Double = value

    fun reset() {
        value = -1.0
    }
}
